package com.meaningful.design_pattern.chain_of_responsibility.validation;

import java.util.ArrayList;
import java.util.List;

public class ValidatorContext {

    private final List<String> errorMessageList = new ArrayList<>();

    public boolean stop;

    private int index;

    private Object value;

    public ValidatorContext(Object value) {
        this.value = value;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    public void appendError(String errorMsg) {
        errorMessageList.add(errorMsg);
    }

    public void throwExceptionIfNecessary() throws ValidatorException {
        if (errorMessageList.isEmpty()) {
            return;
        }
        throw new ValidatorException(errorMessageList.toString());
    }

    public void stopChain() {
        this.stop = true;
    }

    public boolean shouldStop() {
        return this.stop;
    }

    public void doNext(Object value) {
        index++;
        this.value = value;
    }

    public int currentIndex() {
        return this.index;
    }

}
